package board;

import java.awt.*;

import sgf.SGFParseException;
import sgf.SGFUtils;
import ui.GobanPanel;

/**
 * This class represents a line on the Board
 * @author TKington
 *
 */
public class LineMark extends NodeMark {
	//	TODO: remove LineMark entirely?
	/** The stroke for a normal line. */
    private static final Stroke basicStroke = new BasicStroke(3);
    /** The stroke for a dashed line. */
    private static final Stroke dashedStroke = new BasicStroke(3, 
                                                               BasicStroke.CAP_SQUARE,
                                                               BasicStroke.JOIN_MITER,
                                                               10,
                                                               new float[] {10, 10},
                                                               0);
    /** The SGF tag type of the line. */
    private String tag;
    /** The color of the line. */
    private Color color;
    /** The starting point of the line. */
    private Point p1;
    /** The endpoint of the line. */
    private Point p2;
    /** The stroke for drawing the line. */
    private Stroke stroke;
    
    /**
     * Creates a LineMark that draws a line between two points.
     * @param tag The SGF tag used to create the line
     * @param p1 the first point
     * @param p2 the second point
     */
    public LineMark(String tag, Point p1, Point p2) {
        type = LINE;
        this.tag = tag;
        if(tag.equals("LN")) //$NON-NLS-1$
            color = Color.blue;
        else if(tag.equals("LR")) //$NON-NLS-1$
            color = Color.red;
        
        if(tag.equals("LS")) { //$NON-NLS-1$
            color = Color.black;
            stroke = dashedStroke;
        }
        else stroke = basicStroke;
        
        
        this.p1 = p1;
        this.p2 = p2;
    }
    
    @Override
	public Object clone() {
    	return new LineMark(tag, new Point(p1), new Point(p2));
    }
    
    @Override
	public boolean equals(Object o) {
        if(!(o instanceof LineMark))
            return false;
        
        LineMark m = (LineMark)o;
        if(!p1.equals(m.p1))
            return false;
        if(!p2.equals(m.p2))
            return false;
        if(!color.equals(m.color))
            return false;
        return true;
    }
    
    /**
     * Returns the color of the line.
     * @return the Color
     */
    public Color getColor() { return color; }
    
    /**
     * Returns the stroke used to draw the line.
     * @return the Stroke
     */
    public Stroke getStroke() { return stroke; }
    
    /**
     * Returns the starting point of the line.
     * @return the starting point
     */
    public Point getP1() { return p1; }
    
    /**
     * Returns the ending point of the line.
     * @return the ending point
     */
    public Point getP2() { return p2; }
    
    @Override
	public void validatePoints(int size) throws SGFParseException {
        SGFUtils.checkPoint(p1, size);
        SGFUtils.checkPoint(p2, size);
    }
    
    @Override
	public void invalidatePanel(GobanPanel panel) {
        panel.repaint();
    }
    
    @Override
	public Rectangle getBounds() {
        Rectangle r = new Rectangle(p1);
        r.add(p2);
        return r;
    }
    
    /**
     * Returns the SGF tag of the line.
     * @return the SGF tag
     */
    public String getTag() { return tag; }
}
